using AiQiuQuan.Sport.Model.Enums;
using System.ComponentModel.DataAnnotations;

namespace AiQiuQuan.Sport.Model.Models
{
    /// <summary>
    /// 新增比赛dto
    /// </summary>
    public class GameAddDto : IValidatableObject
    {
        /// <summary>
        /// 比赛名称
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "比赛名称必填")]
        [StringLength(255, ErrorMessage = "比赛名称不能超过255个字符")]
        public string Name { get; set; }

        /// <summary>
        /// 比赛类型：1水平比赛、2年龄比赛、3团体比赛
        /// </summary>
        [Required(ErrorMessage = "比赛类型：1水平比赛、2年龄比赛、3团体比赛必填")]
        [EnumDataType(typeof(GameTypeEnum))]
        public GameTypeEnum Type { get; set; }

        /// <summary>
        /// 所属省
        /// </summary>
        public Guid? FollowProvinceId { get; set; }

        /// <summary>
        /// 所属市
        /// </summary>
        public Guid? FollowCityId { get; set; }

        /// <summary>
        /// 场馆ID
        /// </summary>
        [Required(ErrorMessage = "场馆ID必填")]
        public Guid VenueId { get; set; }

        /// <summary>
        /// 开始日期
        /// </summary>
        [Required(ErrorMessage = "开始日期必填")]
        public DateTimeOffset StartDate { get; set; }

        /// <summary>
        /// 开始时间:08:00
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "开始时间:08:00必填")]
        [StringLength(8, ErrorMessage = "开始时间:08:00不能超过8个字符")]
        public string StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "结束时间必填")]
        [StringLength(8, ErrorMessage = "结束时间不能超过8个字符")]
        public string EndTime { get; set; }

        /// <summary>
        /// 小组赛角逐名次：1，2
        /// </summary>
        [Required(ErrorMessage = "小组赛角逐名次：1，2必填")]
        public int GroupRank { get; set; }

        /// <summary>
        /// 淘汰赛角逐名次：2，4
        /// </summary>
        [Required(ErrorMessage = "淘汰赛角逐名次：2，4必填")]
        public int KnockoutRank { get; set; }

        /// <summary>
        /// 最大参赛队伍数
        /// </summary>
        [Required(ErrorMessage = "最大参赛队伍数必填")]
        public int LimitCount { get; set; }

        /// <summary>
        /// 比赛图片
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "比赛图片必填")]
        [StringLength(255, ErrorMessage = "比赛图片不能超过255个字符")]
        public string ImgUrl { get; set; }

        /// <summary>
        /// 价格
        /// </summary>
        [Required(ErrorMessage = "价格必填")]
        [Range(0.01, 999999, ErrorMessage = "价格最低0.01")]
        public decimal Price { get; set; }

        /// <summary>
        /// 组织者
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "组织者必填")]
        [StringLength(255, ErrorMessage = "组织者不能超过255个字符")]
        public string Host { get; set; }

        /// <summary>
        /// 组织者电话
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "组织者电话必填")]
        [StringLength(20, ErrorMessage = "组织者电话不能超过20个字符")]
        public string Phone { get; set; }

        /// <summary>
        /// 组织者图片
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "组织者图片必填")]
        [StringLength(255, ErrorMessage = "组织者图片不能超过255个字符")]
        public string Logo { get; set; }

        /// <summary>
        /// 比赛说明
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "比赛说明必填")]
        [StringLength(255, ErrorMessage = "比赛说明不能超过255个字符")]
        public string Remark { get; set; }

        /// <summary>
        /// 比赛类型
        /// </summary>
        [Required]
        [MinLength(1)]
        public List<GameVersusEnum> VersusTypeList { get; set; }

        /// <summary>
        /// 网球水平，0不限制
        /// 单打：2.5,3.5,4.5,5.5,0
        /// 双打：5.0,7.0,9.0,11,0
        /// </summary>
        public decimal? TennisLevel { get; set; }

        /// <summary>
        /// 年龄,对应比赛年龄选项：8，10，13，15，18
        /// </summary>
        public int? Age { get; set; }

        /// <summary>
        /// 0男、1女、2混合、3不限
        /// </summary>
        public GameSexEnum? GameSex { get; set; }

        /// <summary>
        /// 双打是否一块报名
        /// </summary>
        [Required(ErrorMessage = "双打是否一块报名必填")]
        public bool IsTogether { get; set; }

        /// <summary>
        /// validate
        /// </summary>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (!TimeSpan.TryParse(StartTime, out var start))
            {
                yield return new ValidationResult("开始时间不正确");
                yield break;
            }

            if (!TimeSpan.TryParse(EndTime, out var end))
            {
                yield return new ValidationResult("结束时间不正确");
                yield break;
            }

            if (start >= end)
            {
                yield return new ValidationResult("开始时间大于结束时间");
                yield break;
            }

            var teamCountList = new int[] { 6, 8, 16, 24, 32 };
            if (!teamCountList.Contains(LimitCount))
            {
                yield return new ValidationResult("参数队伍数不正确");
                yield break;
            }

            if (Price <= 0)
            {
                yield return new ValidationResult("金额为0");
                yield break;
            }

            if (GroupRank != 1 && GroupRank != 2)
            {
                yield return new ValidationResult("小组出线数：1，2");
                yield break;
            }

            if (KnockoutRank != 2 && KnockoutRank != 4)
            {
                yield return new ValidationResult("淘汰赛角逐名称：2，4");
                yield break;
            }

            if (Type != GameTypeEnum.Team)
            {
                if (VersusTypeList.Count != 1)
                {
                    yield return new ValidationResult("非团体赛,对阵项目类型数量不为1");
                    yield break;
                }

                var versusType = VersusTypeList[0];
                if (versusType != GameVersusEnum.Single && versusType != GameVersusEnum.Double)
                {
                    yield return new ValidationResult("非团体赛,对阵项目类型不正确");
                    yield break;
                }
            }

            foreach (var item in VersusTypeList)
            {
                if (VersusTypeList.Count(m => m == item) >= 2)
                {
                    yield return new ValidationResult("比赛项目对阵类型重复");
                    yield break;
                }
            }
        }
    }
}
